package org.example.jindie;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.kingdee.bos.webapi.entity.IdentifyInfo;
import com.kingdee.bos.webapi.sdk.K3CloudApi;

import java.util.List;
import java.util.Set;

/**
 * 金蝶测试
 */
public class JinDieTest {

    public static void main(String[] args) {

        String mapJson="{\"FId\":\"金蝶表单id\",\"FCustMatName\":\"客户物料名称\",\"FDeliveryDeptID\":\"发货部门\",\"FReceiverDistrict\":\"收货人地区\",\"FUNJOINQTY\":\"未勾稽数量（作废）\",\"FOwnerID\":\"货主\",\"FMEID\":\"MEID\",\"FSNUnitID\":\"序列号单位\",\"FSECJOININSTOCKQTY\":\"关联入库数量（辅单位）\",\"FETHIRDBILLNO\":\"第三方单据编号\",\"FARJOINAMOUNT\":\"关联应收金额\",\"FBillAllAmount\":\"价税合计\",\"FTransferBizType\":\"跨组织业务类型\",\"FBillAmount_LC\":\"金额（本位币）\",\"FSumRetStockQty\":\"累计退货数量\",\"FCreMonControlOver\":\"信用月结超标\",\"FSettleID\":\"结算方\",\"FValidateStatus\":\"检验确认\",\"FReceiverName\":\"收货人\",\"FReceiverState\":\"收货人省份\",\"FThirdBillNo\":\"第三方单据编号\",\"FEntity_Link_FLnk1TrackerId\":\"迁移图\",\"FRealQty\":\"实发数量\",\"FBillTaxAmount_LC\":\"税额（本位币）\",\"FParentRowId\":\"父行标识\",\"FEntity_Link_FLnkTrackerId\":\"迁移图\",\"FEntity_Link_FAuxUnitQtyOld\":\"原始携带量\",\"FReceiverCountry\":\"收货人国家\",\"FAmount\":\"金额\",\"FTraceStatus\":\"物流状态\",\"FCreChkAmount\":\"工作流信用超标金额\",\"FCorrespondOrgId\":\"对应组织\",\"FAuxUnitQty\":\"库存辅单位数量\",\"FCostAmount_LC\":\"总成本(本位币)\",\"FStockBaseDen\":\"库存基本分母\",\"FSalCostPrice\":\"销售成本价\",\"FLinkPhone\":\"联系电话\",\"FStockLocID\":\"仓位\",\"FReceiveEntryId\":\"收料单分录ID\",\"FMaterialName\":\"物料名称\",\"FPRICEBASEQTY\":\"计价基本数量\",\"FTaxRateId\":\"税率名称\",\"FEntity_Link_FPRICEBASEQTYOld\":\"原始携带量\",\"FSrcType\":\"源单类型\",\"FEntryTaxRate\":\"税率%\",\"FGYDATE\":\"管易发货日期\",\"FOUTCONTROL\":\"控制出库数量\",\"FEntity_Link_FSBillId\":\"源单内码\",\"FBefDisAmt\":\"折前金额\",\"FGYENTERTIME\":\"管易发货时间\",\"FBaseReturnQty\":\"关联退货数量(基本单位)\",\"FIsOverLegalOrg\":\"组织间结算跨法人标识\",\"FBaseUnitQty\":\"库存基本数量\",\"FRetailSaleProm\":\"是否零售促销\",\"FReceiveBillno\":\"收料单编码_序号\",\"FRepairQty\":\"补货数量\",\"FCreChkOverAmount\":\"工作流信用逾期超标额度\",\"FPURBASEJOININSTOCKQTY\":\"关联入库数量（采购基本）\",\"FEntity_Link_FBaseUnitQty\":\"修改携带量\",\"FVmiBusinessStatus\":\"VMI业务\",\"FAuxPropId\":\"辅助属性\",\"FKeeperTypeID\":\"保管者类型\",\"FScanBox\":\"序列号上传\",\"FExtAuxUnitId\":\"辅单位\",\"FServiceContext\":\"服务上下文\",\"FCostPercent\":\"计入成本比例%\",\"FTaxCombination\":\"税组合\",\"FEntity_Link_FBaseUnitQtyOld\":\"原始携带量\",\"FCrePreBatAndMonStatus\":\"审批流信用压批月结检查\",\"FVIPCODE\":\"会员编码\",\"FBFLowId\":\"业务流程\",\"FLotPickFlag\":\"批号拣货结果标识\",\"FCostAmount\":\"计入成本金额\",\"FInStockEntryId\":\"入库单分录ID\",\"FBaseSumRetNoticeQty\":\"累计退货通知数量(销售基本)\",\"FSalUnitID\":\"销售单位\",\"FMateriaModel\":\"规格型号\",\"FIMEI1\":\"IMEI1\",\"FBillTypeID\":\"单据类型\",\"FJoinInStockQty\":\"关联入库数量\",\"FFullyJoined\":\"完全勾稽（作废）\",\"FRowType\":\"产品类型\",\"FSRCBIZUNITID\":\"携带的主业务单位\",\"FIMEI2\":\"IMEI2\",\"FBaseARJoinQty\":\"关联应收数量（计价基本）\",\"FSALECHANNEL\":\"销售渠道\",\"FSysPrice\":\"系统定价\",\"FMaterialID\":\"物料编码\",\"FLinkMan\":\"收货人姓名\",\"FSalBaseARJoinQty\":\"关联应收数量（销售基本）\",\"FReserveEntryId\":\"预留锁库EntryId\",\"FEntryTaxAmount\":\"税额\",\"FSettleOrgID\":\"结算组织\",\"FArrivalStatus\":\"到货确认\",\"FWRITEOFFAMOUNT\":\"冲销金额\",\"FOwnerTypeIdHead\":\"货主类型\",\"FCarriageNO\":\"运输单号\",\"FCarryBillNo\":\"物流单号\",\"FStockerGroupID\":\"库存组\",\"FParentMatId\":\"父项产品\",\"FEntity_Link_FSTableId\":\"源单表内码\",\"FCreditCheckResult\":\"信用检查结果\",\"FGYTAGNAME\":\"管易订单标记名称\",\"FInvoicedQty\":\"累计开票数量(作废)\",\"FStockBaseARJoinQty\":\"关联应收数量（库存基本）\",\"FSerialId\":\"序列号\",\"FSettleTypeID\":\"结算方式\",\"FProPrice\":\"门店供货价\",\"FStockOrgId\":\"发货组织\",\"FJoinedAmount\":\"勾稽金额（作废）\",\"FIsCreateProDoc\":\"是否生成产品档案\",\"FNote\":\"备注\",\"FPrice\":\"单价\",\"FArrivalConfirmor\":\"到货确认人\",\"FCDateOffsetUnit\":\"创建日期偏移单位\",\"FRowARStatus\":\"行关联应收状态\",\"FBuyerWithholding\":\"买方代扣代缴\",\"FStockBaseSumRetStockQty\":\"累计退货数量（库存基本）\",\"FMaterialID_Sal\":\"物料（销售组织）\",\"FConvertEntryID\":\"库存状态转换单ENTRYID\",\"FProduceDate\":\"生产日期\",\"FETHIRDBILLID\":\"第三方单据ID\",\"FCarryBillNoType\":\"类型\",\"FIsRepair\":\"是否补货\",\"FSOEntryId\":\"销售订单EntryId\",\"FInventoryQty\":\"当前库存\",\"FSalBaseNum\":\"销售基本分子\",\"FQualifyType\":\"质量类型\",\"FEntrynote\":\"备注\",\"FTraceTime\":\"时间\",\"FKeeperID\":\"保管者\",\"FLocalCurrID\":\"本位币\",\"FExpiryDate\":\"有效期至\",\"FPhoneNumber\":\"寄件人手机号码\",\"FPRESETBASE1\":\"预设基础资料字段1\",\"FReceiverContactID\":\"收货方联系人\",\"FBaseMustQty\":\"基本单位应发数量\",\"FPRESETBASE2\":\"预设基础资料字段2\",\"FSumRetNoticeQty\":\"累计退货通知数量\",\"FDisPriceQty\":\"拆单数量（计价）\",\"FWRITEOFFPRICEBASEQTY\":\"冲销数量(计价基本)\",\"FBaseInvoicedQty\":\"关联开票数量(基本单位)(作废)\",\"FBussinessType\":\"业务类型\",\"FThirdEntryId\":\"管易订单单号\",\"FSoorDerno\":\"订单单号\",\"FSettleCurrID\":\"结算币别\",\"FAllAmount_LC\":\"价税合计（本位币）\",\"FSALBASEQTY\":\"销售基本数量\",\"FPriceListEntry\":\"行价目表\",\"FReceiverPhone\":\"收货人电话\",\"FStockFlag\":\"库存更新标识\",\"FFrom\":\"起始地点\",\"FBillAmount\":\"金额\",\"FDate\":\"日期\",\"FEntity_Link_FLnk1Amount\":\"数量FLnk1\",\"FSETTLECustomerID\":\"结算组织客户\",\"FLogComId\":\"物流公司\",\"FBaseSumRetstockQty\":\"累计退货数量(基本单位)\",\"FDisassemblyFlag\":\"拆单新单标识\",\"FPriceCoefficient\":\"价格系数\",\"FEntity_Link_FRuleId\":\"转换规则\",\"FOwnerTypeID\":\"货主类型\",\"FESettleCustomerId\":\"明细结算组织客户\",\"FMtoNo\":\"计划跟踪号\",\"FEntity_Link_FSALBASEQTYOld\":\"原始携带量\",\"FBillCostAmount\":\"总成本(暂未使用)\",\"FAllAmount\":\"价税合计\",\"FReceiverMobile\":\"收货人手机\",\"FBASEARQTY\":\"累计应收数量（销售基本）\",\"FLot\":\"批号\",\"FAmount_LC\":\"金额（本位币）\",\"FPriceDiscount\":\"单价折扣\",\"FPickDeptId\":\"捡货部门\",\"FBaseSumInvoicedQty\":\"累计开票数量(计价基本)(作废)\",\"FProAmount\":\"供货金额\",\"FEntryCostAmount\":\"总成本\",\"FEntity_Link_FLnkAmount\":\"数量FLnk\",\"FExchangeTypeID\":\"汇率类型\",\"FBaseUnitID\":\"基本单位\",\"FSettleBySon\":\"按子项结算\",\"FGYFINSTATUS\":\"管易是否到账\",\"FARStatus\":\"关联应收状态\",\"FStockStatusID\":\"库存状态\",\"FSN\":\"SN\",\"FActQty\":\"实收数量\",\"FVAT\":\"增值税\",\"FDiscountListId\":\"折扣表\",\"FProjectNo\":\"项目编号\",\"FBeforeDisPriceQty\":\"拆单前原计价数量\",\"FTailDiffFlag\":\"尾差处理标识\",\"FTraceDetail\":\"物流详情\",\"FBuyerNick\":\"会员名称\",\"FIsPriceExcludeTax\":\"价外税\",\"FCheckDelivery\":\"发货检验\",\"FSerialNo\":\"序列号\",\"FEOwnerSupplierId\":\"明细货主供应商\",\"FWRITEOFFSALEBASEQTY\":\"冲销数量(销售基本)\",\"FEntity_Link_FFlowId\":\"业务流程图\",\"FCarrierID\":\"承运商\",\"FValidateDate\":\"检验日期\",\"FLimitDownPrice\":\"最低限价\",\"FSellerWithholding\":\"卖方代扣代缴\",\"FCustomerID\":\"客户\",\"FPriceUnitId\":\"计价单位\",\"FSerialNote\":\"备注\",\"FARNOTJOINQTY\":\"未关联应收数量（计价单位）\",\"FExpiryPeriod\":\"保质期\",\"FAuxUnitID\":\"库存辅单位\",\"FJoinStatus\":\"行勾稽状态（作废）\",\"FOwnerIdHead\":\"货主\",\"FCostPrice\":\"成本价（本位币）\",\"FOwnerSupplierID\":\"货主供应商\",\"FReceivaDate\":\"应收单业务日期\",\"FCancelStatus\":\"作废状态\",\"FQmEntryID\":\"库存请检单EntryID\",\"FCreateDate\":\"创建日期\",\"FBillAllAmount_LC\":\"价税合计（本位币）\",\"FThirdSrcType\":\"第三方系统来源\",\"FIsFree\":\"是否赠品\",\"FTaxRate\":\"税率%\",\"FTo\":\"终止地点\",\"FUnJoinAmount\":\"未勾稽金额（作废）\",\"FAllDisCount\":\"整单折扣额\",\"FBillTaxAmount\":\"税额\",\"FJoinedQty\":\"勾稽数量（作废）\",\"FSHOPNUMBER\":\"网店编码\",\"FSumInvoicedAMT\":\"累计开票金额(作废)\",\"FSalesGroupID\":\"销售组\",\"FDiscountRate\":\"折扣率%\",\"FGYTAGCODE\":\"管易订单标记编码\",\"FEntity_Link_FSId\":\"源单分录内码\",\"FBomID\":\"BOM版本\",\"FDiscount\":\"折扣额\",\"FDelTime\":\"发货时间\",\"FBillNo\":\"单据编号\",\"FReceiptConditionID\":\"收款条件\",\"FLogisticsNos\":\"物流单号\",\"FCreChkDays\":\"工作流信用超标天数\",\"FSumInvoicedQty\":\"累计应收数量（销售）\",\"FWRITEOFFSTOCKBASEQTY\":\"冲销数量(库存基本)\",\"FEntity_Link_FSALBASEQTY\":\"修改携带量\",\"FReturnQty\":\"关联退货数量\",\"FTaxAmount_LC\":\"税额（本位币）\",\"FExpiryPeriodUnit\":\"保质期单位\",\"FReceiveAddress\":\"收货方地址\",\"FModifierId\":\"最后修改人\",\"FEntity_Link_FSTableName\":\"源单表\",\"FSALUNITQTY\":\"销售数量\",\"FCancellerID\":\"作废人\",\"FBaseJoinInStockQty\":\"关联入库数量(基本单位)\",\"FBefDisAllAmt\":\"折前价税合计\",\"FCreChkStatus\":\"工作流信用检查状态\",\"FBOMEntryId\":\"BOM分录内码\",\"FPlanRecAddress\":\"交货明细执行地址(后台用)\",\"FIsReplaceOut\":\"是否替代出库\",\"FMateriaType\":\"物料类别\",\"FSrcBillNo\":\"源单编号\",\"FCrePreBatchOver\":\"信用压批超标\",\"FApproveDate\":\"审核日期\",\"FStockBaseReturnQty\":\"关联退货数量（库存基本）\",\"FDocumentStatus\":\"单据状态\",\"FPriceUnitQty\":\"计价数量\",\"FARJoinQty\":\"关联应收数量\",\"FBarcode\":\"零售条形码\",\"FPushReturnNotice\":\"下推退货通知标识\",\"FEntity_Link_FPRICEBASEQTY\":\"修改携带量\",\"FPriceListId\":\"价目表\",\"FEntity_Link_FAuxUnitQty\":\"修改携带量\",\"FIsIncludedTax\":\"是否含税\",\"FISGENFORIOS\":\"跨组织结算生成\",\"FUnitID\":\"库存单位\",\"FSECRETURNQTY\":\"关联退货数量（辅单位）\",\"FSumReceivedAMT\":\"累计收款金额\",\"FB2CORDERDETAILID\":\"B2C订单明细Id\",\"FPRESETASSISTANT2\":\"预设辅助资料字段2\",\"FArrivalDate\":\"到货日期\",\"FMustQty\":\"应发数量\",\"FSaleDeptID\":\"销售部门\",\"FPRESETASSISTANT1\":\"预设辅助资料字段1\",\"FRowId\":\"行标识\",\"FApproverID\":\"审核人\",\"FGYFINDate\":\"管易到账时间\",\"FEntity_Link_FFlowLineId\":\"推进路线\",\"FCDateOffsetValue\":\"创建日期偏移量\",\"FCancelDate\":\"作废日期\",\"FReceiptTime\":\"签收时间\",\"FBranchId\":\"销售门店\",\"FSignQty\":\"签收数量\",\"FSaleOrgId\":\"销售组织\",\"FTaxNetPrice\":\"净价\",\"FAllAmountExceptDisCount\":\"价税合计（折前）\",\"FValidateConfirmor\":\"检验确认人\",\"FTaxAmount\":\"税额\",\"FStockerID\":\"仓管员\",\"FExchangeRate\":\"汇率\",\"FEntity_Link_FLnk1SState\":\"上游状态\",\"FRefuseQty\":\"拒收数量\",\"FARAMOUNT\":\"累计应收金额\",\"FReceiverID\":\"收货方\",\"FPayerID\":\"付款方\",\"FModifyDate\":\"最后修改日期\",\"FInStockBillno\":\"入库单编码_序号\",\"FSalesManID\":\"销售员\",\"FIsInterLegalPerson\":\"组织间结算跨法人标识\",\"FReplaceMaterialID\":\"替代原物料编码\",\"FGenFromPOS_CMK\":\"零售单日结生成\",\"FCustMatID\":\"客户物料编码\",\"FExtAuxUnitQty\":\"实发数量(辅单位)\",\"FTaxPrice\":\"含税单价\",\"FEntity_Link_FLnkSState\":\"上游状态\",\"FReceiverAddress\":\"收货人地址\",\"FReceiverCity\":\"收货人城市\",\"FCreatorId\":\"创建人\",\"FThirdBillId\":\"第三方单据平台单号\",\"FIsTotalServiceOrCost\":\"整单服务或费用\",\"FHeadLocationId\":\"交货地点\",\"FSNQty\":\"序列号单位数量\",\"FBillCostAmount_LC\":\"总成本(本位币)(暂未使用)\",\"FIsConsumeSum\":\"消耗汇总\",\"FStockID\":\"仓库\"}";
        JSONObject jsonObject = JSONUtil.parseObj(mapJson);
        Set<String> fieldList = jsonObject.keySet();


        //fieldList 按照 , 分割
        String[] fieldListArr = fieldList.toString().split(",");

        IdentifyInfo iden = new IdentifyInfo();


//        iden.setUserName("沈俊杰");
//        iden.setAppId("277491_Q/0JQYvHzlDeX+Ss24WqzawK1I6a0PPE");
//        iden.setdCID("1373031008873496576");
//        iden.setAppSecret("ec53784952b94411bccca7d586cd21aa");
//        iden.setlCID(2052);
//        iden.setServerUrl("https://api.kingdee.com/galaxyapi");


//        iden.setUserName("赖永达");
//        iden.setAppId("279493_6e4OSyjLzNGU1UwpXexC2bSt7gS8RAtL");
//        iden.setdCID("20190116161745");
//        iden.setAppSecret("7321c68cdcb54511879307eff3001737");
//        iden.setlCID(2052);
//        iden.setServerUrl("https://api.kingdee.com/galaxyapi");


        iden.setUserName("周保坤");
        iden.setAppId("282695_2Z8uSbksUpo9QbzL1ZTPzZWG1vWXxALK");
        iden.setdCID("64a158f29b6199");
        iden.setAppSecret("3533b17476684a64bb710ceec7927b23");
        iden.setlCID(2052);
        iden.setServerUrl("https://94me2ij82028.vicp.fun/k3cloud");


//        iden.setUserName("Administrator");
//        iden.setAppId("279618_539p7xFv0IBeQUWu32xqSw+L5r076LnE");
//        iden.setdCID("60e3d32a1c4b8a");
//        iden.setAppSecret("3532b7a56006401aa7dee7d349b73033");
//        iden.setlCID(2052);
//        iden.setServerUrl("http://36.153.33.195:8090/k3cloud");


        K3CloudApi client = new K3CloudApi(iden);
//请求参数，要求为json字符串
        String jsonData = "{\n" +
                "    \"FormId\": \"SAL_OUTSTOCK\",\n" +
                "    \"FieldKeys\": \"" + fieldList + "\",\n" +
                "    \"TopRowCount\": 1,\n" +
                "    \"StartRow\": 0,\n" +
                "    \"Limit\": 2000,\n" +
                "    \"FilterString\": [\n" +
                "        {\n" +
                "            \"Left\": \"(\",\n" +
                "            \"FieldName\": \"FDate\",\n" +
                "            \"Compare\": \">\",\n" +
                "            \"Value\": \"2024-07-01 00:00:00\",\n" +
                "            \"Right\": \")\",\n" +
                "            \"Logic\": \"0\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"Left\": \"(\",\n" +
                "            \"FieldName\": \"FDate\",\n" +
                "            \"Compare\": \"<\",\n" +
                "            \"Value\": \"2024-07-06 00:00:00\",\n" +
                "            \"Right\": \")\",\n" +
                "            \"Logic\": \"0\"\n" +
                "        }\n" +
                "    ]\n" +
                "}";
        try {
            //调用接口
            List<List<Object>> lists = client.executeBillQuery(jsonData);
            for (List<Object> list : lists) {//list是一行数据
                for (int i = 0; i < list.size(); i++) {
                    System.out.printf("%s: %s\n", fieldListArr[i].trim(), list.get(i));
                }
                System.out.println("====================================");

            }


        } catch (Exception e) {
            System.out.printf("调用接口失败: %s\n", e.getMessage());
        }


    }


}
